package es.ats.codington.web.controllers;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.VisitorService;



@Controller
public class VisitorController {

	@RequestMapping(value = "/validate_login.htm")
	public ModelAndView validateLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ModelAndView modelAndView = new ModelAndView();
		try{
			int isAdmin = 0;
			VisitorService visitor=new VisitorService();
			String username=request.getParameter("user");
			String pass=request.getParameter("pass1");
			
			int i=visitor.loginDAOService(username, pass);
			if(i==1){
				Cookie loginCookie = new Cookie("user", username);
				isAdmin = visitor.isAdminService(username);
				Cookie adminCookie = new Cookie("isAdmin", String.valueOf(isAdmin));
				loginCookie.setMaxAge(30*60);
				response.addCookie(loginCookie);
				adminCookie.setMaxAge(30*60);
				response.addCookie(adminCookie);
				modelAndView.setViewName("index2");
			}
			else {
				String error = "Invalid username or password";
				request.setAttribute("invalid_login", error);
				modelAndView.setViewName("login");
			}

		} catch (Exception e) {
			String error = "Invalid username or password";
			request.setAttribute("invalid_login", error);
			request.setAttribute("error", "Unexpected error.");
			modelAndView.setViewName("login");
		}
		
		return modelAndView;
	}
	
	@RequestMapping(value = "/visitor_Register.htm")
	public ModelAndView visitorRegister(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ModelAndView modelAndView = new ModelAndView();
		VisitorService visitorS=new VisitorService();
		String fname=request.getParameter("nombre");
		String lname=request.getParameter("apell");
		String username=request.getParameter("visitor");
		String email=request.getParameter("email");
		String dni=request.getParameter("dni");
		String nphone=request.getParameter("phone");
		String address=request.getParameter("adress");
		String pass=request.getParameter("pass1");
		
		Visitor visitor=new Visitor(fname,lname,username,email,dni,nphone,address,pass, false);
		int i=visitorS.insertVisitorService(visitor);
		
		if(i==1){
			modelAndView.setViewName("index");
		}
		else {
			modelAndView.setViewName("visitorRegister");
		}
		return modelAndView;
	}
	
	@RequestMapping(value = "/infVisitor.htm")
	public ModelAndView infVisitor(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ModelAndView modelAndView = new ModelAndView();
		String visitorName=null;
		Visitor visitor = new Visitor();
		VisitorService visitorService = new VisitorService();
		Cookie cookie= null;
		Cookie[] cookies=request.getCookies();
		if(cookies!=null){
			for(int i=0; i<cookies.length; i++){
				cookie=cookies[i];
				if(cookie.getName().compareTo("user")==0)
				visitorName=cookie.getValue();
			}
		}
		visitor = visitorService.getVisitorService(visitorName);
		request.setAttribute("visitor", visitor);
		modelAndView.setViewName("editUser");
		return modelAndView;
		
	}
	
	@RequestMapping(value = "/editUser.htm")
	public ModelAndView edituser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ModelAndView modelAndView = new ModelAndView();
		String userName=null;
		Cookie[] cookies = request.getCookies();
		if (cookies != null){
			for(Cookie cookie : cookies){
				if(cookie.getName().equals("user")){
					userName=cookie.getValue();
					break;
				}
			}
		}
		if(userName ==null){
			modelAndView.setViewName("login");
		}
		VisitorService visitorS=new VisitorService();
		
		response.setContentType("text/html");
		
		String fname=request.getParameter("nombre");
		String lname=request.getParameter("apell");
		String email=request.getParameter("email");
		String dni=request.getParameter("dni");
		String nphone=request.getParameter("phone");
		String address=request.getParameter("adress");
		String pass="cas";
		
		Visitor visitor=new Visitor(fname,lname,userName,email,dni,nphone,address,pass, false);
				int i=visitorS.updateVisitorSevice(visitor, userName);
		
		if(i==1){
			modelAndView.setViewName("index2");
		}
		else {
			modelAndView.setViewName("editUser");
		}
		return modelAndView;
	}
	
	@RequestMapping(value = "/updatePassword.htm")
	public ModelAndView passwordUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ModelAndView modelAndView = new ModelAndView();
		VisitorService visitorService = new VisitorService();
		String userName = null; 
		Cookie[] cookies = request.getCookies();
		if (cookies != null){
			for(Cookie cookie : cookies){
				if(cookie.getName().equals("user")){
					userName=cookie.getValue();
					break;
				}
			}
		}
		
		String oldpass=request.getParameter("oldpass");
		String newpass1=request.getParameter("newpass1");
		
		int i = visitorService.updatePasswordService(newpass1, oldpass, userName);
		if (i == 1){
			modelAndView.setViewName("index2");
		}else{
			modelAndView.setViewName("passwordUpdate");
		}
		return modelAndView;
	}
}
